package Fail3_17;

import java.io.*;
import java.util.Scanner;

public class FileDemo9 {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入您需要复制的文件路径");
        String str=scanner.next();
        File file=new File(str);
        if(!file.exists()){
            System.out.println("地址有误");
            return ;
        }
        System.out.println("请输入目标地址");
        String str1=scanner.next();
        File file1=new File(str1);
        if(file1.exists()){
            System.out.println("文件已经存在，程序退出");
            return;
        }
        if(!file1.getParentFile().exists()){
            System.out.println("文件不存在，程序退出");
        }
        try (InputStream inputStream=new FileInputStream(file);
             OutputStream outputStream=new FileOutputStream(file1)){
            while (true) {
                //从文件里面读1024
                byte[] bytes = new byte[1024];
                int len = inputStream.read(bytes);
                if (len == -1) {
                    break;
                }
                outputStream.write(bytes,0,len);
            }
            //这里的flash不加也没啥，当被close的时候 会自动进行flash
            outputStream.flush();
        }catch (IOException e){
            e.printStackTrace();
        }
        System.out.println("复制完成");
    }
}
